//
//  MeetingController.m
//  电子相册
//
//  Created by zxy on 2019/1/23.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "MeetingController.h"
//测试接口
#define url_M1 @"http://open.qyer.com/qyer/footprint/placehome?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&page=1&track_app_channel=App%2520Store&track_app_version=7.4&track_device_info=iPhone%25206&track_deviceid=7B2E86D8-EB88-4036-B818-9EF729EF7FCB&track_os=ios%25209.3.1&v=1"
@interface MeetingController ()<TZImagePickerControllerDelegate,SDPhotoBrowserDelegate>
@property(nonatomic,strong)NSMutableArray *dataSoure;
@end

@implementation MeetingController
static NSString *const MeetingReuseIdentifier = @"MeetingReuseIdentifier";

-(NSMutableArray *)dataSoure{
    if(!_dataSoure){
        _dataSoure = [NSMutableArray array];
    }
    return _dataSoure;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.tag = 1;
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([EalbumsPhotoCell class]) bundle:nil] forCellWithReuseIdentifier:MeetingReuseIdentifier];
    //测试数据
    [self requestDatas];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshDatas:) name:refreshControllerDatasName object:nil];
}

#pragma mark -刷新控制器数据
-(void)refreshDatas:(NSNotification *)no{
    NSInteger idx = [no.object integerValue];
    if(idx == self.view.tag){
        [self requestDatas];
    }
}

#pragma mark -请求数据
-(void)requestDatas{
    [self.dataSoure removeAllObjects];
    [PPNetworkHelper GET:url_M1 parameters:nil success:^(id responseObject) {
        NSArray *array = responseObject[@"data"];
        NSDictionary *dic = [array objectAtIndex:1];
        NSArray *array1 = dic[@"tagdata"][@"citylist"];
        for (NSInteger i = 0; i < array1.count; i++) {
            NSDictionary *dict = array1[i];
            NSString *imgUrl = dict[@"photo"];
            [self.dataSoure addObject:imgUrl];
        }
        NSInteger idx = self.dataSoure.count % 3;
        NSInteger idx1 = self.dataSoure.count / 3;
        NSInteger idx2 = idx ? (idx1 + 1) : (idx1);
        CGFloat height = ((screenWidth - 20) / 3 + 5) * idx2;
        [self.mjHeaderRefresh endRefreshing];
        self.collectionView.xmg_height = height;
        [self.collectionView reloadData];
        [[NSNotificationCenter defaultCenter] postNotificationName:changeContainControllerCellHeightName object:@(height)];
    } failure:^(NSError *error) {
        NSLog(@"error=%@",error);
    }];
}

-(void)upLoadBtnClick:(NSNotification *)no{
    //获取当前点击按钮下标
    NSInteger idx = [no.object integerValue];
    if(idx == self.view.tag){
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
        [self presentViewController:imagePickerVc animated:YES completion:nil];
    }
}

#pragma mark -完成图片选择
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
    NSLog(@"%@",photos);
    [self requestDatas];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSoure.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EalbumsPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MeetingReuseIdentifier forIndexPath:indexPath];
    cell.phototUrl = self.dataSoure[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    
    //图片的父控件
    browser.sourceImagesContainerView = self.collectionView;
    //图片数量
    browser.imageCount = self.dataSoure.count;
    //当前图片下标
    browser.currentImageIndex = indexPath.item;
    
    browser.delegate = self;
    
    [browser show]; // 展示图片浏览器
}

// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    NSString *urlStr = self.dataSoure[index];
    return [NSURL URLWithString:urlStr];
}


@end
